package com.xcx.minservice.controller;

import com.xcx.commonuntils.returnuntls.ReturnMessage;

import com.xcx.minservice.entity.FileSystem;
import com.xcx.minservice.request.GetUserFilesRequest;
import com.xcx.minservice.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Api(tags = "文件管理api")
@RestController
@RequestMapping("/minio")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
@CrossOrigin
    public class MinioController {

        final  private MinioService minioService;


        @ApiOperation(value = "用户文件上传", httpMethod = "POST")
        @ApiImplicitParams({
                @ApiImplicitParam(value = "上传文件(仅需要文件名）", name = "file", required = true, allowMultiple = true, dataType = "MultipartFile"),
                @ApiImplicitParam(value = "用户id", name = "userId", required = true),
                @ApiImplicitParam(value = "文件类型/1-img,2-audio,3-video,4-doc", name = "source", required = true,dataType = "Int")
        })
        @PostMapping(value = "upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
        public ReturnMessage<Object> upload(@RequestPart("file") MultipartFile file,@RequestParam("userId") String userId,@RequestParam("source") Integer source) {

            return minioService.upload(file,userId,source);
        }


    @ApiOperation(value = "批量文件上传", httpMethod = "POST")
    @PostMapping(value = "uploads", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiImplicitParams({

            @ApiImplicitParam(paramType = "form", name = "files", value = "文件数组(使用postman测试)", allowMultiple = true, dataType = "MultipartFile[]")

    })
    public ReturnMessage<List<String>> uploads(MultipartFile[] files) {

        return minioService.uploads(files);
    }

    @ApiOperation(value = "文件预览")
    @GetMapping(value = "preview")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件预览", name = "fileName", required = true,example = "img/d10ad9adfc3d4706861acef1d9038ad6.png")
    })
    public ReturnMessage<Object> preview(String fileName) {

        return minioService.preview(fileName);
    }


    @ApiOperation(value = "文件下载")
    @GetMapping("download")
    public void download(String fileName, HttpServletResponse res) {

        minioService.download(fileName,res);
    }



    @ApiOperation(value = "文件删除")
    @DeleteMapping("remove")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件删除", name = "fileName", required = true,example = "img/d10ad9adfc3d4706861acef1d9038ad6.png")
    })
    public ReturnMessage<Object> remove(String fileName) {

        return minioService.remove(fileName);
    }



    @ApiOperation(value = "查询用户所有文件信息", httpMethod = "POST")
    @PostMapping(value = "getUserFiles")
    public ReturnMessage<List<FileSystem>> getUserFiles(@RequestBody GetUserFilesRequest request) {

        return minioService.getUserFiles(request);
    }























}